<?php

namespace XWAM\Addon\POrgUser\Controller;

/**
 * RoleController.class 控制器
 * @since 1.0 <2015-5-23> SoChishun <14507247@qq.com> Added.
 */
class RoleController extends \XWAM\Controller\AppbaseController {

    /**
     * role_list操作
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function role_list() {
        $list = AddonD('Role')->get_paging_list(false, $show, array('ORDERBY' => 'id'));
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display_cpp();
    }

    public function get_role_tree_json() {
        $list = AddonD('Role')->field('id, title')->select();
        $this->ajaxReturn($list);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function role_export() {
        if (!$this->isSuperAdmin) {
            die('没有权限!');
        }
        $search_data = $this->get_role_search_data();
        $list = D('Role')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    public function role_copy($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '参数无效');
        }
        AddonD('Role')->copy_role($id);
        $this->ajaxMsg(true);
    }

    /**
     * role_edit操作
     * @param int $id 主键编号
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function role_edit($id = 0) {
        if ($id) {
            $this->assign('data', AddonD('Role')->find($id));
        }
        $this->display();
    }

    /**
     * role_edit_save操作
     * @since 1.0<2015-5-23> SoChishun Added.
     */
    public function role_edit_save() {
        $result = AddonD('Role')->save_role();
        $this->dialogJump($result['status'],$result['info']);
    }

    /**
     * role_delete操作
     * @param string $id 主键编号
     * @since 1.0<2015-7-31> SoChishun Added.
     */
    public function role_delete($id = '') {
        $this->ajaxReturn(AddonD('Role')->delete_role($id));
    }

    public function role_permission_edit($id = 0) {
        $this->assign('id', $id);
        $rules = AddonD('Role')->where(array('id' => $id))->getField('rules');
        $tree=D('Rule')->select_tree(array(),array('CHECKBOX'=>true,'VALUES'=>$rules));
        $this->assign('tree',$tree);
        $this->display();
    }

    public function role_permission_edit_save() {
        $ids = I('ids');
        $role_id = I('id');
        if (!$role_id || !$ids) {
            $this->result(false, '参数有误');
        }
        $result = M('tuser_role')->where(array('id' => $role_id))->setField('permission_rule', implode(',', $ids));
        $this->result($result, '保存%');
    }

}
